package com.sskj.gocoinapp.ui.activity;


import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.github.yoojia.inputs.AndroidNextInputs;
import com.github.yoojia.inputs.StaticScheme;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.InputUtil;
import com.sskj.common.util.TimeUtil;
import com.sskj.gocoinapp.R;
import com.sskj.gocoinapp.presenter.PwdForgetActivityPresenter;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.TabItem;

import java.util.ArrayList;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

@Route(path = RConfig.APP_PWD_FORGET)
public class PwdForgetActivity extends BaseActivity<PwdForgetActivityPresenter> {
    @BindView(R.id.commonTabLayout)
    CommonTabLayout commonTabLayout;
    @BindView(R.id.etAccount)
    EditText etAccount;
    @BindView(R.id.ivAccountClear)
    ImageView ivAccountClear;
    @BindView(R.id.etCode)
    EditText etCode;
    @BindView(R.id.tvGetCheckCode)
    TextView tvGetCheckCode;
    @BindView(R.id.etPwd)
    EditText etPwd;
    @BindView(R.id.ivPwdToggle)
    ImageView ivPwdToggle;
    @BindView(R.id.etPwdAgain)
    EditText etPwdAgain;
    @BindView(R.id.ivPwdToggleAgain)
    ImageView ivPwdToggleAgain;
    @BindView(R.id.btSubmit)
    Button btSubmit;
    private boolean isMobile = true;
    private Disposable timeDis;
    private AndroidNextInputs inputs;
    private AndroidNextInputs inputsCheckCode;
    private boolean isSee;
    private boolean isSeeAgain;

    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_pwd_forget;
    }

    @Override
    public PwdForgetActivityPresenter getPresenter() {
        return new PwdForgetActivityPresenter();
    }

    @Override
    public boolean getImmersion() {
        return false;
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.appJavaPwdForgetActivity1));
        ArrayList<CustomTabEntity> tabItems = new ArrayList<>();
        tabItems.add(new TabItem(App.INSTANCE.getString(R.string.appJavaPwdForgetActivity2), 0, 0));
        tabItems.add(new TabItem(App.INSTANCE.getString(R.string.appJavaPwdForgetActivity3), 0, 0));
        commonTabLayout.setTabData(tabItems);
        commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                switch (position) {
                    case 0:
                        if (isMobile)
                            return;
                        changeMobile();
                        break;
                    case 1:
                        if (!isMobile)
                            return;
                        changeEmail();
                        break;
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        ClickUtil.click(200, ivPwdToggleAgain, () -> {
            isSeeAgain = !isSeeAgain;
            etPwdAgain.setTransformationMethod(isSeeAgain ? HideReturnsTransformationMethod.getInstance() : PasswordTransformationMethod.getInstance());
            ivPwdToggleAgain.setImageResource(isSeeAgain ? R.mipmap.app_icon_show : R.mipmap.app_icon_hide);
        });
        ClickUtil.click(200, ivPwdToggle, () -> {
            isSee = !isSee;
            etPwd.setTransformationMethod(isSee ? HideReturnsTransformationMethod.getInstance() : PasswordTransformationMethod.getInstance());
            ivPwdToggle.setImageResource(isSee ? R.mipmap.app_icon_show : R.mipmap.app_icon_hide);

        });
        inputs = new AndroidNextInputs();
        inputs.clear();
        inputs
                .add(etAccount, StaticScheme.NotBlank())
                .add(etCode, StaticScheme.NotBlank())
                .add(etPwd, StaticScheme.NotBlank())
                .add(etPwdAgain, StaticScheme.NotBlank())
        ;
        inputsCheckCode = new AndroidNextInputs();
        inputsCheckCode.clear();
        ClickUtil.click(ivAccountClear, () -> {
            etAccount.setText("");
        });
        ClickUtil.click(tvGetCheckCode, () -> {
            if (isMobile) {
                inputsCheckCode.clear();
                inputsCheckCode
                        .add(etAccount, StaticScheme.NotBlank(), InputUtil.Mobile());
            } else {
                inputsCheckCode.clear();
                inputsCheckCode
                        .add(etAccount, StaticScheme.NotBlank(), StaticScheme.Email());
            }
            if (inputsCheckCode.test()) {
                tvGetCheckCode.setEnabled(false);
                mPresenter.sendCode(etAccount.getText().toString());
            }
        });
        ClickUtil.click(btSubmit, () -> {
            if (inputs.test()) {
                mPresenter.resetLoginPwd(etAccount.getText().toString(),
                        etPwd.getText().toString(),
                        etPwdAgain.getText().toString(),
                        etCode.getText().toString()
                );
            }
        });
    }

    private void changeEmail() {
        isMobile = false;
        etAccount.setText("");
        etAccount.setHint(App.INSTANCE.getString(R.string.appJavaPwdForgetActivity6));

    }

    private void changeMobile() {
        isMobile = true;
        etAccount.setText("");
        etAccount.setHint(App.INSTANCE.getString(R.string.appJavaPwdForgetActivity8));
    }

    public void sendCheckCodeSuccess() {
        DisposUtil.close(timeDis);
        timeDis = TimeUtil.newTime(this)
                .subscribe(aLong -> {
                    if (aLong > 0) {
                        tvGetCheckCode.setText(aLong + "s");
                    } else {
                        tvGetCheckCode.setText(App.INSTANCE.getString(R.string.appJavaPwdForgetActivity10));
                        tvGetCheckCode.setEnabled(true);
                    }
                });
    }

    @Override
    protected void onDestroy() {
        DisposUtil.close(timeDis);
        super.onDestroy();
    }

    public void sendCheckCodeFail() {
        tvGetCheckCode.setEnabled(true);
    }

    public void resetPwdSuccess() {
        finish();
    }
}
